﻿using ConfExpert.Forms.Base;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevTeam;

using System;
using System.Data;

namespace ConfExpert.Forms.Depo.Kargo
{
    public partial class KargoDuzenle : XForm
    {
        int kargoId;
        public KargoDuzenle(int kargoId)
        {
            InitializeComponent();
            this.kargoId = kargoId;
        }

        private void KargoDuzenle_Load(object sender, EventArgs e)
        {
            
            bbiUpdate.ItemClick += bbiUpdate_ItemClick;
            gv.Init("GetKargoRow", this.kargoId);
            gv.MakeColumnInvisible("KargoId");
            if (gv.GetFocusedDataRow()["CariId"].ToInt() == 0)
                gv.MakeColumnInvisible("CariId");
            else
                gv.MakeColumnInvisible("To");

            //RepositoryItemComboBox riWhoPays = new RepositoryItemComboBox();
            //riWhoPays.TextEditStyle = TextEditStyles.DisableTextEditor;
            //riWhoPays.Items.AddRange(new object[] { "KARBEL", "ALICI" });
            //gv.Columns["WhoPays"].ColumnEdit = riWhoPays;

            DataTable dtKargo = Session.DataManager.ExecuteDataTableByQuery("select * from KargoFirma");
            RepositoryItemGridLookUpEdit riKargoFirm = GetCustomGLE(dtKargo, "Name", "KargoFirmaId");
            riKargoFirm.TextEditStyle = TextEditStyles.DisableTextEditor;
            gv.Columns["KargoFirmaId"].ColumnEdit = riKargoFirm;

            gv.SetCaptions("KargoFirmaId:KargoFirma", "Pieces:Miktar", "Note:Kargo Notu", "To:Kime");

            gv.BestFitColumns();
        }

        private void bbiUpdate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gv.CloseEditor();
            gv.EndEditing();
            DataRow dr = gv.GetFocusedDataRow();

            if ((int)dr["kargoId"] == this.kargoId)
            {
                int cariId = 0;
                string to = null;
                if (gv.GetFocusedDataRow()["CariId"].ToInt() == 0)
                    to = dr["To"].ToStr();
                else
                    cariId = dr["cariId"].ToInt(-1);
                int kargoFirmaId = (int)dr["KargoFirmaId"];
                string wayBillNo = dr["WayBillNo"].ToStr();
                DateTime shipmentDate = dr["ShipmentDate"].ToDate();
                //string whoPays = dr["WhoPays"].ToStr();
                int pieces = dr["Pieces"].ToInt() < 0 ? 0 : dr["Pieces"].ToInt();
                string Note = dr["Note"].ToStr();

                ExecuteNonQuery("UpdKargoRow", cariId, to, kargoFirmaId, wayBillNo, shipmentDate, pieces, Note, this.kargoId);
                this.Close();
            }
        }
    }
}